#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include "util_alive_check.h"

static void *test1(void *arg)
{
    int count = 0;
    int index = alive_check_add(__FUNCTION__, 5,TYPE_HAS_DEADING);
    sleep(15);
    while (1)
    {
        alive_check_beat(index);
        sleep(3);
        if (count % 17 == 0)
        {
            sleep(16);
        }
        count++;
    }
}

static void *test2(void *arg)
{
    int count = 0;
    int index = alive_check_add(__FUNCTION__, 6,TYPE_HAS_DEADING);
    sleep(17);
    while (1)
    {
        sleep(2);
        alive_check_beat(index);
        if (count % 25 == 0)
        {
            sleep(17);
        }
        count++;
    }
}

static void *test3(void *arg)
{
    int count = 0;
    int index = alive_check_add(__FUNCTION__, 4,TYPE_NO_DEADING);
    sleep(15);
    while (1)
    {
        sleep(2);
        alive_check_beat(index);
        if (count % 10 == 0)
        {
            sleep(18);
        }
        count++;
    }
}

static void *test4(void *arg)
{
    int count = 0;
    int index = alive_check_add(__FUNCTION__, 8,TYPE_NO_DEADING);
    sleep(15);
    while (1)
    {
        sleep(4);
        alive_check_beat(index);
        if (count % 16 == 0)
        {
            sleep(30);
        }
        count++;
    }
}

int main(void)
{
    int count = 0;
    pthread_t th_a, th_b, th_c, th_d;
    void *retval;
    alive_check_init(NULL);
    // pthread_create(&th_a, NULL, test1, 0);
    // pthread_create(&th_b, NULL, test2, 0);
    pthread_create(&th_c, NULL, test3, 0);
    // pthread_create(&th_d, NULL, test4, 0);

    while (1)
    {
        alive_check_items(count++);
        sleep(1);
        if(count % 5 == 0) {
            alive_check_print_states(NULL);
        }
    }
    pthread_join(th_a, &retval);
    pthread_join(th_b, &retval);
    pthread_join(th_c, &retval);
    pthread_join(th_d, &retval);
    return 0;
}